package com.jsoup;
import java.io.IOException;   
import java.io.UnsupportedEncodingException;   
import java.net.MalformedURLException;   
  
import org.jsoup.Jsoup;   
import org.jsoup.nodes.Document;   
import org.jsoup.nodes.Element;   
import org.jsoup.select.Elements;   
  
  
/**  
* 获取6间房视频  
* @author honny.huang  
* 2011-3-31  
* http://hi.juziku.com/sunlightcs/  
*/  
public class JsoupParseCN6 {   
    private static String URL = "http://6.cn/watch/14757577.html";   
    public static void main(String[] args) throws Exception{   
           
        Document doc = getURLContent();   
           
           
        System.out.println("视频标题："+doc.title());   
           
        /**  
         * 获取视频缩略图  
         */  
        Element picEt = doc.getElementsByClass("summary").first();   
        String pic = picEt.getElementsByTag("img").first().attr("src");   
        System.out.println("视频缩略图："+pic);   
           
           
        String time = getVideoTime(doc,"watchUserVideo");   
           
        if(time==null){   
            time = getVideoTime(doc,"watchRelVideo");   
        }   
        System.out.println("视频时长："+time);   
           
           
        /**  
         * 获取视频地址  
         */  
        Element flashEt = doc.getElementById("video-share-code");   
        doc = Jsoup.parse(flashEt.attr("value"));     
        String flash = doc.select("embed").attr("src");   
           
        System.out.println("视频地址："+flash);   
           
    }   
  
    /**  
     * 获取视频时长      
     */  
    private static String getVideoTime(Document doc, String id) {   
        String time = null;   
           
        Element timeEt = doc.getElementById(id);    
        Elements links = timeEt.select("dt > a");   
           
           
        for (Element link : links) {   
          String linkHref = link.attr("href");   
          if(linkHref.equalsIgnoreCase(URL)){   
              time = link.parent().getElementsByTag("em").first().text();   
              break;   
          }   
        }   
        return time;   
    }   
       
    /**  
     * 获取6间房网页的内容  
     */  
    private static Document getURLContent() throws MalformedURLException, IOException, UnsupportedEncodingException {   
        Document doc = Jsoup.connect(URL)   
          .data("query", "Java")   
          .userAgent("Mozilla")   
          .cookie("auth", "token")   
          .timeout(3000)   
          .post();   
        return doc;   
    }   
  
}  
